from struct import unpack
class Mutable:pass
music = Mutable()
music.data = open( 'data/5113NC3R/sound.bin', 'rb' ).read()
music.count = unpack( '<I', music.data[:4] )[0]
music.size = unpack( '<I', music.data[4:8] )[0]
remainder = len( music.data )
clip_number = 0
music.clips = []

while remainder > music.size:
    clip = Mutable()
    start = 8 + clip_number * 96 + 0
    end = 8 + clip_number * 96 + 4
    clip.present = unpack( '<I', music.data[start:end] )[0]
    start = 8 + clip_number * 96 + 4
    end = 8 + clip_number * 96 + 20
    clip.name = unpack( '<ssssssssssssssss', music.data[start:end] )
    start = 8 + clip_number * 96 + 20
    end = 8 + clip_number * 96 + 24
    clip.offset = unpack( '<I', music.data[start:end] )[0]
    start = 8 + clip_number * 96 + 24
    end = 8 + clip_number * 96 + 28
    clip.length = unpack( '<I', music.data[start:end] )[0]
    remainder = len( music.data ) - end
    clip_number += 1
    music.clips.append( clip )

x=0
for clip in music.clips:
    start = 8 + 96 * music.count + clip.offset
    end = 8 + 96 * music.count + clip.offset + clip.length
    clip.data = music.data[start:end]
    name = ''.join(clip.name).replace('\x00','')
    if name == 'ammo03.wav' :print x, clip.length
    x+=1
